{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "#%% md\r\n",
    "# 第一部分"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 1. 如何判断一个对象是可迭代对象，迭代器或生成器\r\n",
    "from collections import Iterable,Iterator,Generator\r\n",
    "def is_or_not(object):\r\n",
    "    if isinstance(a,Iterable):\r\n",
    "        print(\"这是可迭代对象\")\r\n",
    "    elif isinstance(a,Iterator):\r\n",
    "        print(\"这是迭代器对象\")\r\n",
    "    elif isinstance(a,Generator):\r\n",
    "        print(\"这是生成器对象\")\r\n",
    "    return\r\n",
    "\r\n",
    "# 2. 迭代器与生成器的区别\r\n",
    "'''\r\n",
    "任何实现了```__iter__()```和```__next__()```都是迭代器\r\n",
    "生成器是一种特殊的迭代器，生成器拥有比迭代器更加简洁的语法\r\n",
    "'''\r\n",
    "\r\n",
    "# 3. 可迭代对象与迭代器的区别\r\n",
    "'''\r\n",
    "凡是有```__iter__```并且这个方法返回一个迭代器的对象都称之为可迭代对象\r\n",
    "任何实现了```__iter__()```和```__next__()```都是迭代器\r\n",
    "容器类型都是可迭代对象\r\n",
    "迭代器一定是可迭代对象，反之不成立\r\n",
    "\r\n",
    "凡是可作用于for循环的对象都是Iterable类型；\r\n",
    "凡是可作用于next()函数的对象都是Iterator类型，它们表示一个惰性计算的序列；\r\n",
    "'''\r\n",
    "\r\n",
    "# 4. 如何定义一个生成器\r\n",
    "def generator(i):\r\n",
    "    yield i\r\n",
    "a = generator(1)\r\n",
    "print(type(a))\r\n",
    "\r\n",
    "# 5. 如何获取可迭代对象、迭代器与生成器中的数据\r\n",
    "'''\r\n",
    "可迭代对象用for循环\r\n",
    "迭代器用next()函数\r\n",
    "生成器也是用for循环\r\n",
    "'''"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#%% md\r\n",
    "# 第二部分"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "loc = input(\"请输入文件所在路径与文件名\")\r\n",
    "print(max(len(line.strip()) for line in open(loc)))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#%% md\r\n",
    "# 第三部分"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "\r\n",
    "#1\r\n",
    "import datetime\r\n",
    "import re\r\n",
    "start = datetime.date(2000,1,1)\r\n",
    "end = datetime.date(2021,7,29)\r\n",
    "# 用以可用于计算的一天  timedelta代表两个时间之间的时间差\r\n",
    "def get_days(start,end):\r\n",
    "    days = re.findall(r'([0-9]+) days',str(end-start))[0]\r\n",
    "    return (start,\"与\",end,\"相差\",days,\"天\")\r\n",
    "\r\n",
    "#2\r\n",
    "def get_before(start,days):\r\n",
    "    days = datetime.timedelta(days=days)\r\n",
    "    before = start+days\r\n",
    "    return before\r\n",
    "\r\n",
    "#3\r\n",
    "import os\r\n",
    "\r\n",
    "# -*- encoding: utf-8 -*-\r\n",
    "import os\r\n",
    "\r\n",
    "import os\r\n",
    "from os.path import join, getsize\r\n",
    "\r\n",
    "def getdirsize(dir):\r\n",
    "    #dir为文件夹的绝对路径\r\n",
    "    size = 0\r\n",
    "    for root, dirs, files in os.walk(dir):\r\n",
    "        size += sum([getsize(join(root, name)) for name in files])\r\n",
    "    print(\"该文件夹大小为：\",size,\"字节\")\r\n",
    "    return"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}